package com.tanhua.server.controller;

import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.service.SettingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author Pan Rundong
 * @Date 2023-02-27 9:47
 * @Desc 设置表Controller
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class SettingsController {
    @Autowired
    private SettingsService settingsService;

    /**
     * 获取用户通用设置
     *
     * @return org.springframework.http.ResponseEntity
     * @Author: Pan Rundong
     */
    @GetMapping("/settings")
    public ResponseEntity<SettingsVo> getSettings() {
        SettingsVo settingsVo = settingsService.getSettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置问题：
     * 如果有则更新
     * 如果没有则保存
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     * @Author: Pan Rundong
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        settingsService.questions(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改设置
     *
     * @param map
     * @return {@link ResponseEntity}
     * @Author Pan Rundong
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity changeSettings(@RequestBody Map map) {
        Settings settings = new Settings();
        settings.setLikeNotification((Boolean) map.get("likeNotification"));
        settings.setPinglunNotification((Boolean) map.get("pinglunNotification"));
        settings.setGonggaoNotification((Boolean) map.get("gonggaoNotificationNotification"));
        settingsService.update(settings);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单
     *
     * @param page
     * @param size
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.PageResult>
     * @Author Pan Rundong
     */
    @GetMapping("/blacklist")
    public ResponseEntity<PageResult> getBlackList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageResult pageResult = settingsService.getBlackList(page, size);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 移除黑名单
     *
     * @param
     * @return org.springframework.http.ResponseEntity
     * @Author Pan Rundong
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
        //1.调用service
        settingsService.deleteBlackList(blackUserId);
        //2.返回
        return ResponseEntity.ok(null);
    }
}
